const express = require('express')
const router = express.Router()
const connection = require('.././mysql')


router.get('/getUserList', (req, res) => {
    const sql = 'SELECT * FROM users'

    connection.query(sql, (err, result) => {
        if (err) {
            console.log(err)
            res.status(500).send('database error')
        } else {
            res.send(result)
        }
    })
})


router.post('/deleteUser', (req, res) =>{
    const {id} = req.body.params;

    const sql = `DELETE FROM users WHERE id = ${id}`

    connection.query(sql, (err, result) => {
        if (err) {
            console.log(err)
            res.status(500).send('database error')
        } 

        if (result.affectedRows === 0) {
            res.status(404).send('未找到此用户')
        } else {
            res.send({
                status: 200,
                msg: '删除成功'
            })
        }
    })
})



module.exports = router